package com.aliothservice.catfish.validation;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class RequestValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return true;
	}

	@Override
	public void validate(Object object, Errors errors) {
		if (PhoneNumberValidation.class.isAssignableFrom(object.getClass())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phoneNumber", "400", "missing phone number");
		}
		if (PasswordValidation.class.isAssignableFrom(object.getClass())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "400", "missing password");
			PasswordValidation passwordValidation = (PasswordValidation)object;
			String password = passwordValidation.getPassword();
			if ((6>password.length()) || (18<password.length())) {
				errors.rejectValue("password", "401", "password too short or too long");
			}
		}
		if (TokenValidation.class.isAssignableFrom(object.getClass())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "token", "400", "missing token");
		}
		if (VerificationCodeValidation.class.isAssignableFrom(object.getClass())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "verificationCode", "400", "missing verification code");
		}
	}

}
